/**
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.qlangtech.tis.compiler.java;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.util.Map;

/**
 * @author 百岁（baisui@qlangtech.com）
 * @date 2019年6月6日
 */
public class MyJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {

    private final Map<String, IOutputEntry> fileObjects;

    protected MyJavaFileManager(JavaFileManager fileManager, Map<String, IOutputEntry> fileObject) {
        super(fileManager);
        this.fileObjects = fileObject;
    }

    @Override
    public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
        IOutputEntry javaFileObject = fileObjects.get(className);
        if (javaFileObject == null) {
            super.getJavaFileForInput(location, className, kind);
        }
        return javaFileObject.getFileObject();
    }

    @Override
    public JavaFileObject getJavaFileForOutput(Location location
            , String qualifiedClassName, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
        IOutputEntry javaObj = fileObjects.get(qualifiedClassName);
        if (javaObj != null) {
            return javaObj.getFileObject();
        }
        NestClassFileObject fileObj = NestClassFileObject.getNestClassFileObject(qualifiedClassName, this.fileObjects);
        return fileObj;
    }

}
